from django.core.mail import send_mail
from config.emailconfig import EMAIL_HOST_USER
from celery_tasks.celery_main import app
import logging

from users.views import logger

logging = logging.getLogger(__name__)
# app.autodiscover_tasks(["celery_tasks.email"]) ===> tasks===>@app.task("send_mail_task")
# 注册任务函数
@app.task(name = "send_mail_task")
def send_mail_task(email,html_message):

    try:
        # 完成邮件发送的逻辑
        res = send_mail(
            subject="jutong32商城",
            message="",  # 默认为空
            from_email=EMAIL_HOST_USER,  # 发件人
            recipient_list=[email],
            html_message=html_message  # 邮箱正文
        )
    except Exception as e:
        logging.error(f"通知邮件发送异常{e},收件人[{email}]")
    # 如果邮箱发送成功 res =  1  反之  res = 0
    else:
        if res:
            logger.info(f"邮件发送成功收件人[{email}]")
        else:
            logger.warning(f"邮件发送失败收件人[{email}]")